 <template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车牌号" prop="license">
        <el-input v-model="queryParams.license" placeholder="请输入车牌号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="入库时间" prop="enterTime">
        <el-date-picker clearable v-model="queryParams.entertime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择入库时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="车辆类型">
        <el-select v-model="queryParams.cartype" placeholder="请选择车辆类型">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="当前状态">
        <el-select v-model="queryParams.ispay" placeholder="请选择">
          <el-option v-for="item in options_ispay" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-printer" size="mini" @click="handlePrint">打印</el-button>
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:precords:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="precordsList" @selection-change="handleSelectionChange">
      <el-table-column label="车辆ID" align="center" prop="pid" />
      <el-table-column label="车牌号" align="center" prop="license" />
      <el-table-column label="入库时间" align="center" prop="entertime" />
      <el-table-column label="出库时间" align="center" prop="outtime" />
      <el-table-column label="车辆类型" align="center" prop="cartype" />
      <el-table-column label="缴费金额" align="center" prop="payment" />
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改车辆信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listPrecords,
    getPrecords,
    delPrecords,
    addPrecords,
    updatePrecords
  } from "@/api/system/precords";

  export default {
    name: "Precords",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 车辆信息表格数据
        precordsList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          license: null,
          entertime: null,
          cartype: null,
          ispay: 1,
        },
        options: [{
          value: '小车',
          label: '小车'
        }, {
          value: '大车',
          label: '大车'
        }, {
          value: '新能源',
          label: '新能源'
        }, ],
        options_ispay: [{
          value: 1,
          label: '所有车辆'
        }, {
          value: 0,
          label: '停在库内'
        },  ],
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询车辆信息列表 */
      getList() {
        this.loading = true;
        listPrecords(this.queryParams).then(response => {
          this.precordsList = response.rows;
          this.total = response.total;
          this.loading = false;
          console.info(response.rows[0].outtime);

          var i = 0;
          while (this.precordsList[i] != null) {
            if (this.precordsList[i].ispay == 1) {
              this.precordsList[i].ispay = "已缴费";
            } else {
              this.precordsList[i].ispay = "未缴费";
            }
            i = i + 1;
          }
          console.info(this.precordsList[0].ispay);
        });

      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          pid: null,
          license: null,
          entertime: null,
          outTime: null,
          cartype: null,
          enterPicPath: null,
          outPicPath: null,
          isPay: null,
          deleted: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },

      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.queryParams.license = null;
        this.queryParams.entertime = null;
        this.queryParams.cartype = null;
        this.queryParams.ispay = 1;
        this.handleQuery();
      },

      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.pid)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加车辆信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const pid = row.pid || this.ids
        getPrecords(pid).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改车辆信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.pid != null) {
              updatePrecords(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addPrecords(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
       /** 打印按钮操作 */
        handlePrint() {
          // 获取表格元素
          const printContent = document.querySelector('.el-table');

          // 创建一个新的窗口对象，用于打印
          const printWindow = window.open('', '', 'height=600,width=800');

          // 将表格内容复制到新窗口中
          printWindow.document.write('<html><head><title>车辆信息打印</title>');
          printWindow.document.write('</head><body >');
          printWindow.document.write(printContent.outerHTML);
          printWindow.document.write('</body></html>');

          // 确保所有资源都加载完毕
          printWindow.document.close();

          // 调用打印函数
          printWindow.focus();
          printWindow.print();
          printWindow.close();
        },
      /** 删除按钮操作 */
      handleDelete(row) {
        const pids = row.pid || this.ids;
        this.$modal.confirm('是否确认删除车辆信息编号为"' + pids + '"的数据项？').then(function() {
          return delPrecords(pids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/precords/export', {
          ...this.queryParams
        }, `precords_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
